package com.league.zjxn.main.home

import android.os.Bundle
import android.webkit.WebSettings
import android.webkit.WebViewClient
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.league.zjxn.R
import com.league.zjxn.entity.SameDetails
import com.league.zjxn.jetpack.vmodel.SameViewModel
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_details_same.*
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.select.Elements


class SameDetailsActivity : OwnerViewModeActivity<SameViewModel>() {

    companion object {
        private const val EXTRA_KEY_TITLE = "EXTRA_KEY_TITLE"
        private const val EXTRA_KEY_ACTION = "EXTRA_KEY_ACTION"
        private const val EXTRA_KEY_SAME_ID = "EXTRA_KEY_SAME_ID"

        fun openAlliance(title: String? = null, id: String) {
            val bundle = Bundle().apply {
                putString(EXTRA_KEY_ACTION, "Alliance")
                putString(EXTRA_KEY_TITLE, title)
                putString(EXTRA_KEY_SAME_ID, id)
            }
            ActivityUtils.startActivity(bundle, SameDetailsActivity::class.java)
        }

        fun openAbout(title: String? = null, id: String) {
            val bundle = Bundle().apply {
                putString(EXTRA_KEY_ACTION, "About")
                putString(EXTRA_KEY_TITLE, title)
                putString(EXTRA_KEY_SAME_ID, id)
            }
            ActivityUtils.startActivity(bundle, SameDetailsActivity::class.java)
        }

    }

    override fun viewModel(): SameViewModel {
        return createViewModel()
    }

    private val mAction: String?
        get() = intent?.extras?.getString(EXTRA_KEY_ACTION)

    private val mTitle: String?
        get() = intent?.extras?.getString(EXTRA_KEY_TITLE)

    private val mSameId: String
        get() = intent?.extras?.getString(EXTRA_KEY_SAME_ID) ?: "0"

    override val layoutResId: Int
        get() = R.layout.activity_details_same

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, view = flSameDetailsBar)
    }

    override fun initView() {
        flSameDetailsBar.setTitleTextContent(mTitle)
        tvSameDetailsTitle.text = mTitle

        wvSameDetailsWeb.webViewClient = WebViewClient()
        wvSameDetailsWeb.settings.apply {
            //支持缩放，默认为true。
            setSupportZoom(false)
            //调整图片至适合webview的大小
            useWideViewPort = true
            // 缩放至屏幕的大小
            loadWithOverviewMode = true
            //设置默认编码
            defaultTextEncodingName = "utf-8"
            //设置自动加载图片
            loadsImagesAutomatically = true
            //允许访问文件
            allowFileAccess = true
            //开启javascript
            javaScriptEnabled = true
            //支持通过JS打开新窗口
            javaScriptCanOpenWindowsAutomatically = true
            //支持内容重新布局
            layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
            //关闭webview中缓存
            cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
        }
    }

    override fun initData() {
        if (mAction == "Alliance") {
            viewModel.sameAllianceDetails(mSameId)
        } else if (mAction == "About") {
            viewModel.sameAboutDetails(mSameId)
        }
    }

    override fun initViewMode() {
        viewModel.sameDetailsLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })
    }

    private fun fillData(sameDetails: SameDetails) {

        val document: Document = Jsoup.parse(sameDetails.content)
        val elements: Elements = document.select("img")
        val span: Elements = document.select("span")
        span.forEach {
            val style = it.attr("style") + "font-size: 45px;"
            it.attr("style", style)
        }
        elements.forEach {
            val style = "${it.attr("style")}width:100%;"
            it.attr("style", style)
        }
        val realContent = buildString {
            append("<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body>")
            append(document.body().toString())
            append("</body></html>")
        }
        wvSameDetailsWeb.loadDataWithBaseURL("", realContent, "text/html", "utf-8", null)

    }
}